#! /usr/local/bin/python
#-*- coding: utf-8 -*-


"""
pyChatter. A multi-agent based instant messaging written in Python.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

pyChatter  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2010/01/04 $"
__copyright__ = "Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPLv3"

from xml.dom.minidom import parse
from xml.dom.minidom import Document

XML_FILE = "./configurations/clients_informations.xml"

def add_client(client_name, client_password, client_ontology, client_pubkey):
    """
    """
    try:
        xml = parse(XML_FILE)
    except Exception:
        xml = Document()
        root = xml.createElement("clients")
        xml.appendChild(root)

    # test if username already exists
    for client in xml.getElementsByTagName("client"):
        if client_name == client.getElementsByTagName("name")[0].childNodes[0].nodeValue:
            return 'already_registered'

    # add the client
    client = xml.createElement("client")
    xml.getElementsByTagName("clients").item(0).appendChild(client)

    value = xml.createElement("name")
    client.appendChild(value)
    text_node = xml.createTextNode(client_name)
    value.appendChild(text_node)

    value = xml.createElement("password")
    client.appendChild(value)
    text_node = xml.createTextNode(client_password)
    value.appendChild(text_node)

    value = xml.createElement("ontology")
    client.appendChild(value)
    text_node = xml.createTextNode(client_ontology)
    value.appendChild(text_node)

    value = xml.createElement("pubkey")
    client.appendChild(value)
    text_node = xml.createTextNode(client_pubkey)
    value.appendChild(text_node)


    return xml.toxml()

def del_client():
    """
    """
    pass

def get_ontology(client_name):
    """
    """
    try:
        xml = parse(XML_FILE)
    except Exception:
        return None

    for client in xml.getElementsByTagName("client"):
        if client_name == client.getElementsByTagName("name")[0].childNodes[0].nodeValue:
            try:
                return client.getElementsByTagName("ontology")[0].childNodes[0].nodeValue
            except:
                pass
    return None

def get_password(client_name):
    """
    """
    try:
        xml = parse(XML_FILE)
    except Exception:
        return None

    for client in xml.getElementsByTagName("client"):
        if client_name == client.getElementsByTagName("name")[0].childNodes[0].nodeValue:
            try:
                return client.getElementsByTagName("password")[0].childNodes[0].nodeValue
            except:
                pass
    return None

def get_pub_key(client_name):
    """
    """
    try:
        xml = parse(XML_FILE)
    except Exception:
        return None

    for client in xml.getElementsByTagName("client"):
        if client_name == client.getElementsByTagName("name")[0].childNodes[0].nodeValue:
            try:
                return client.getElementsByTagName("pubkey")[0].childNodes[0].nodeValue
            except:
                pass
    return None

def set_ontology():
    """
    """
    pass

def set_pub_key():
    """
    """
    pass

def set_password():
    """
    """
    pass

def write(xml):
    """Save the XML file.
    """
    try:
        file = open(XML_FILE, 'w')
        file.write('%s' % xml.encode('utf-8'))
    except IOError, e:
        print "Writting error"
    finally:
        file.close()


if __name__ == "__main__":
    # Point of entry in execution mode
    #print get_ontology('Bob')
    #print get_password('Bob')
    #print get_pub_key('Bob')
    xml = add_client("Bob", "password", "ontoBob", "pubKeyBob")
    write(xml)
    xml = add_client("Alice", "123456", "ontoAlice", "pubKeyAlice")
    write(xml)